package com.wsb.wetchat.controller;

import com.alibaba.fastjson.JSONObject;
import com.wsb.wetchat.dao.User;
import com.wsb.wetchat.service.UserService;
import com.wsb.wetchat.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wang shaobo
 * @date 2022/6/28  10:05
 */
@RestController
public class RedisController {

    @Autowired
    RedisUtils redisUtils;

    @Autowired
    UserService userService;


    @RequestMapping(value = "/redis/str/set",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    public void setString(@RequestParam(value = "key") String key,
                         @RequestParam(value = "value",required = true) String value){

        redisUtils.set(key, value);


    }



    @RequestMapping(value = "/redis/str/get",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    public String getValue(@RequestParam(name = "key",value = "key",required = true) String key){

        return redisUtils.getValue(key);
    }

    @RequestMapping(value = "/redis/hash/hset",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    public void setHashCach(int id){

        User user = userService.queryById(id);

        JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(user));


        Map userMap = JSONObject.parseObject(JSONObject.toJSONString(user), Map.class);

        redisUtils.setHashCach(id,userMap);

    }

    @RequestMapping(value = "/redis/hash/get",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    public List<Object> getHashCach(@RequestParam(value = "key") String id){


        return redisUtils.getHashCache(id);

    }

    @RequestMapping(value = "/redis/hash/entry",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    public Map getHashEntry(@RequestParam(value = "key") String id){


        return redisUtils.getHashEntry(id);

    }


}
